﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using NUnit.Framework;
using PS.Utils.FPRPC;

namespace PS.WssSync.Tests
{
    [TestFixture]
    public class RPCTests
    {
        private const string testUrl = "http://Saber-test:81/";

        [Test]
        public void Regex()
        {
            string source = "Начало:&#209;&#131;конец";            

            const string decodePattern = "(&#[0-9]{1,3};){2}";
            Regex rg = new Regex(@decodePattern, RegexOptions.Compiled);
            MatchCollection matches = rg.Matches(source);

            foreach (Match match in matches)
            {
                string[] strings = match.Value.Split(new string[] { ";&#" }, StringSplitOptions.None);
                byte[] bytes = {byte.Parse(strings[0].TrimStart(new char[]{'&', '#'})), 
                                byte.Parse(strings[1].TrimEnd(';'))};
                source = source.Replace(match.Value, Encoding.UTF8.GetString(bytes));
            }                   
        }

        [Test]
        public void ListDirectories()
        {
            string url = testUrl + "test";
            FrontPageRPC fpRPC = new FrontPageRPC();

            WebUrl dirUrl = fpRPC.UrlToWebUrl(url);
            List<WebUrl> list = fpRPC.ListFolders(dirUrl);

            Assert.IsNotEmpty(list);
        }

        [Test]
        public void GetAllDirectories()
        {
            FrontPageRPC fprpc = new FrontPageRPC();

            const string siteUrl = "http://saber-test:81/test";            
            const string authorUrl = siteUrl + "/_vti_bin/_vti_aut/author.dll";

//            const string text = "method=open+service%3a12%2e0%2e0%2e6211&service%5fname=%2ftest";
            const string text = "method=list+documents%3a12%2e0%2e0%2e6211&service%5fname=%2ftest&listHiddenDocs=true&listExplorerDocs=false&listRecurse=false&listFiles=true&listFolders=true&listLinkInfo=true&listIncludeParent=true&listDerived=false&listBorders=false&listChildWebs=true&listThickets=false&initialUrl=Docs&folderList=%5bDocs%3bTW%7c13+Nov+2008+10%3a57%3a31+%2d0000%5d";

            string decodedText = HttpUtility.UrlDecode(text);

//            System.Diagnostics.Debug.WriteLine("URL: " + authorUrl);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(authorUrl);            
            req.Method = "POST";
            req.KeepAlive = true;
            req.ContentType = "application/x-www-form-urlencoded";
            req.Headers.Add("X-Vermeer-Content-Type", "application/x-www-form-urlencoded");
            req.Credentials = CredentialCache.DefaultCredentials;

            Stream reqStream = req.GetRequestStream();
            StreamWriter sw = new StreamWriter(reqStream);
            sw.Write(text);            
            sw.Flush();            

            reqStream.Flush();
            reqStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream);
            string responseData = sr.ReadToEnd();
            

            List<string> result = new List<string>();

            Regex urlMatchRegEx = new Regex(@"\<li\>url=(?<urlInfo>.*?)\<li\>", RegexOptions.Compiled | RegexOptions.Singleline);
            MatchCollection urlInfoMatches = urlMatchRegEx.Matches(responseData);

            foreach (Match m in urlInfoMatches)
            {
                string fld = HttpUtility.UrlDecode(m.Value);
                fld = fld.Replace("<li>url=", "");
                fld = fld.Replace("\n<li>", "");                                
                result.Add(fld);                
            }
        }
    }
}